package icu.guodapeng.oauth2.component

import icu.guodapeng.oauth2.domain.user.SecurityUser
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken
import org.springframework.security.oauth2.common.OAuth2AccessToken
import org.springframework.security.oauth2.provider.OAuth2Authentication
import org.springframework.security.oauth2.provider.token.TokenEnhancer
import org.springframework.stereotype.Component

/**
 * JWT 内容增强器
 */
@Component
class JwtTokenEnhancer : TokenEnhancer {
    override fun enhance(accessToken: OAuth2AccessToken, authentication: OAuth2Authentication): OAuth2AccessToken {
        val securityUser = authentication.principal as SecurityUser
        val info = HashMap<String, Any?>()
        info["id"] = securityUser.id
        (accessToken as DefaultOAuth2AccessToken).additionalInformation = info
        return accessToken
    }
}
